package com.zk.nakedfowl.barcodeMachineMonitor.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.*;

import com.zk.nakedfowl.common.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;


import com.zk.nakedfowl.barcodeMachineMonitor.dao.MachineStatusDao;
import com.zk.nakedfowl.barcodeMachineMonitor.pojo.MachineStatus;

/**
 * 服务层
 * 
 * @author Administrator
 *
 */
@Service
public class MachineStatusService {

	@Autowired
	private MachineStatusDao machineStatusDao;
	
	@Autowired
	private IdWorker idWorker;

	/**
	 * 查询全部列表
	 * @return
	 */
	public List<MachineStatus> findAll() {
		return machineStatusDao.findAll();
	}


	/**
	 * 根据createBy查询
	 * @param str
	 * @return
	 */
	public List<MachineStatus> findByCreateBy(String str) {
		Specification<MachineStatus> spec = new Specification<MachineStatus>() {
			@Override
			public Predicate toPredicate(Root<MachineStatus> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
				//查询属性，创建人
				Path<Object> createBy = root.get("createBy");
				//查询方式
				Predicate p = criteriaBuilder.equal(createBy,str);
				return p;
			}
		};
		return machineStatusDao.findAll(spec);
	}

	/**
	 * 条件查询+分页
	 * @param whereMap
	 * @param page
	 * @param size
	 * @return
	 */
	public Page<MachineStatus> findSearch(Map whereMap, int page, int size) {
		Specification<MachineStatus> specification = createSpecification(whereMap);
		PageRequest pageRequest =  PageRequest.of(page-1, size);
		return machineStatusDao.findAll(specification, pageRequest);
	}

	
	/**
	 * 条件查询
	 * @param whereMap
	 * @return
	 */
	public List<MachineStatus> findSearch(Map whereMap) {
		Specification<MachineStatus> specification = createSpecification(whereMap);
		return machineStatusDao.findAll(specification);
	}

	/**
	 * 根据ID查询实体
	 * @param id
	 * @return
	 */
	public MachineStatus findById(String id) {
		return machineStatusDao.findById(id).get();
	}

	/**
	 * 增加
	 * @param machineStatus
	 */
	public void add(MachineStatus machineStatus) {
		machineStatus.setId( idWorker.nextId()+"" );
		machineStatusDao.save(machineStatus);
	}

	/**
	 * 修改
	 * @param machineStatus
	 */
	public void update(MachineStatus machineStatus) {
		machineStatusDao.save(machineStatus);
	}

	/**
	 * 删除
	 * @param id
	 */
	public void deleteById(String id) {
		machineStatusDao.deleteById(id);
	}

	/**
	 * 动态条件构建
	 * @param searchMap
	 * @return
	 */
	private Specification<MachineStatus> createSpecification(Map searchMap) {

		return new Specification<MachineStatus>() {

			@Override
			public Predicate toPredicate(Root<MachineStatus> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();
                // 
                if (searchMap.get("id")!=null && !"".equals(searchMap.get("id"))) {
                	predicateList.add(cb.like(root.get("id").as(String.class), "%"+(String)searchMap.get("id")+"%"));
                }
                // 脚标机编号
                if (searchMap.get("barcodeMachineId")!=null && !"".equals(searchMap.get("barcodeMachineId"))) {
                	predicateList.add(cb.like(root.get("barcodeMachineId").as(String.class), "%"+(String)searchMap.get("barcodeMachineId")+"%"));
                }
                // 在线状态
                if (searchMap.get("state")!=null && !"".equals(searchMap.get("state"))) {
                	predicateList.add(cb.like(root.get("state").as(String.class), "%"+(String)searchMap.get("state")+"%"));
                }
                // 用户id
                if (searchMap.get("userId")!=null && !"".equals(searchMap.get("userId"))) {
                	predicateList.add(cb.like(root.get("userId").as(String.class), "%"+(String)searchMap.get("userId")+"%"));
                }
                // 创建者
                if (searchMap.get("createBy")!=null && !"".equals(searchMap.get("createBy"))) {
                	predicateList.add(cb.like(root.get("createBy").as(String.class), "%"+(String)searchMap.get("createBy")+"%"));
                }
				
				return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));

			}
		};

	}

}
